学习笔记:创建Trie 树存储英文单词及中文意思

<pre class="cpp" name="code">#include <iostream>
#include <string.h>
using namespace std;

//实现目标:用trie tree来存储英文单词及其中文意思

typedef enum
{
	BRANCH_NODE = 0,
	LEAF_NODE
}NK;

typedef struct trieNode
{
    int subNum;
    char* chinese;
    struct trieNode* subTrieNode[26];
}TrieNode, *pTrieNode;


static char* english[10] = {
		"hello",
		"apple",
		"app",
		"help",
		"blue",
		"ultimate",
		"professional",
		"major",
		"idea",
		"professor"
};

static char* chinese[10] = {
		"你好",
		"苹果",
		"应用;应用程序",
		"帮助;救命",
		"蓝色",
		"最终的;极限的",
		"专业的;职业的",
		"专业;主要的",
		"主意;想法",
		"教师;教授"
};

/* 大写字母转换成小写 */
void UpperToLower(char* srcWord)
{
    if(!srcWord)
        return;

    char* src = srcWord;
    
    while(*src != '\0')
    {
        if(*src >= 'A' && *src <= 'Z')
            *src = *src - 'A' + 'a';
        src++;
    }
}


static char tmpWord[50] = {'\0'}; //目前最长的单词没有超过50个字母的

/*
	pStart参数为开始搜索的节点
	word参数为前面已经获得的部分单词字符
	endIndex参数为单词的结束位置
*/
void printSubTrieTreeWords(pTrieNode pStart, char* word, int endIndex)
{
    if(!pStart)
        return;
    if(pStart->chinese != NULL){
		tmpWord[endIndex] = '\0';
        cout << tmpWord << endl;
    }

	if(pStart->subNum == 0)
		return;

    int i = 0; 
    for(; i < 26; i++)
    {
        if(pStart->subTrieNode[i] != NULL)
        {
            char c = i + 'a';
            tmpWord[endIndex] = c;
            printSubTrieTreeWords(pStart->subTrieNode[i], tmpWord, endIndex + 1);
            tmpWord[endIndex] = '\0';
        }
    }
}


bool SearchWordInTrieTree(const char* word, pTrieNode pHead, char* chinese)
{
    if(!word || !pHead || !chinese)
        return false;

    const char* pWord = word;
    pTrieNode pCur = pHead;

    while(*pWord != '\0')
    {
        int idx = *pWord - 'a';
        if(pCur->subTrieNode[idx] == NULL){
            return false;
        }
        pCur = pCur->subTrieNode[idx];
        pWord++;
    }

    if(pCur != NULL)
    {
        if(pCur->chinese != NULL){
            strcpy(chinese, pCur->chinese);
            return true;
        }
        else
        {
            int len = strlen(word);
            strcpy(tmpWord, word);
            tmpWord[len] = '\0';
			cout << "找不到单词: " << word << endl;
            cout << "类似的单词有: " << endl;
            printSubTrieTreeWords(pCur, tmpWord, len);
        }
    }
    return false;
}

bool insertWord(const char* englishWord, const char* chinese, pTrieNode pHead)
{
    if(!englishWord || !chinese || !pHead)
        return false;

    const char* pWord = englishWord;
    pTrieNode pCur = pHead;

    while(*pWord != '\0')
    {
        int idx = *pWord - 'a';
        if(pCur->subTrieNode[idx] == NULL)
        {
            pCur->subTrieNode[idx] = (pTrieNode)malloc(sizeof(TrieNode));
	     memset(pCur->subTrieNode[idx], 0, sizeof(TrieNode));
        }
        pCur->subNum++;
        pCur = pCur->subTrieNode[idx];
        pWord++;
    }

    if(pCur != NULL)
    {
        pCur->chinese = (char*)malloc(sizeof(char) * (strlen(englishWord) + 1));
        strcpy(pCur->chinese, chinese);
    }
    
    return true;
}

pTrieNode init()
{
    pTrieNode pHead = (pTrieNode)malloc(sizeof(TrieNode));
    if(pHead == NULL){
        cout << "malloc pHead fail!!!"<< endl;
        return NULL;
    }

    memset(pHead, 0, sizeof(TrieNode));
    return pHead;
}

void deInit(pTrieNode pHead)
{
    if(pHead != NULL)
    {
        int i = 0;
        if(pHead->subNum != 0){
            for(; i < 26; i++)
            {

                if(pHead->chinese){
                    free(pHead->chinese);
                    pHead->chinese = NULL;
                }
                deInit(pHead->subTrieNode[i]);
            }
        }
        free(pHead);
        pHead = NULL;
    }
}

int main()
{
    pTrieNode pHead = NULL;
    pHead = init();
    for(int i = 0; i < 10; i++)
    {
    	  insertWord(english[i], chinese[i], pHead);
    }

    char word[50];
    char mean[1024];
    while(1)
    {
        cout << "输入要查询的单词: " << endl;
        cin >> word;
        UpperToLower(word);
        if(SearchWordInTrieTree(word, pHead, mean))
        {
            cout << "单词" << word << "的意思是: "  << mean << endl;
        }
        else
        {
#if 0
            cout << "需要存入该单词请输入 yes 否则输入 no " << endl;
            char flag[10];
            cin >> flag;
            if(strncmp(flag, "yes", 3) == 0)
            {
                cout << "请输入该单词的中文意思: " << endl;
                cin >> mean;
                bool ret = insertWord(word, mean, pHead);
                if(ret)
                    cout << "单词" << word << " 保存成功" << endl;
                else
                    cout << "单词" << word << " 保存失败" << endl; 
            }
#endif
        }
    }

    deInit(pHead);
    return 0;
}


 

运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytrie 是一个前缀Trie)数据结构的Python 开发包。在 pytrie 模块中, CharTrie 和 StringTrie 类可以执行一个可变的映射接口。这个工具包具有以下特点:数据的全映射功能支持迭代和删除二级前缀支持前缀检查,以及最长和最短的前缀查找功能可扩展支持任意的用户自定义键Trie 知识点:在计算机科学中,trie,又称前缀字典树,是一种有序,用于保存关联数组,其中的键通常是字符串。与二叉查找不同,键不是直接保存在节点中,而是由节点在中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。 标签:pytrie 分享 window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": [], "bdPic": "", "bdStyle": "1", "bdSize": "24" }, "share": {} }; with (document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' ~(-new Date() / 36e5)];\r\n \r\n \r\n \r\n \r\n \u8f6f\u4ef6\u9996\u9875\r\n \u8f6f\u4ef6\u4e0b\u8f7d\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\nwindow.changyan.api.config({\r\nappid: 'cysXjLKDf', conf: 'prod_33c27aefa42004c9b2c12a759c851039' });

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值